package bb.lanxing.model.database;

import android.text.TextUtils;
//import com.orm.SugarRecord;
//import com.orm.dsl.Table;
//import com.orm.query.Select;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.common.config.Constants;
import bb.lanxing.util.Log;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

public class City extends LitePalTableBase {
    private static final String CITY_INSERT = "INSERT INTO city (id, cityindex, provinceid, name) VALUES ";
    private int cityIndex;
    private String name;
    private int provinceId;

    public static void importDatas2() {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(App.getContext().getAssets().open("city.txt")));
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    return;
                }
                LitePal.getDatabase().execSQL(CITY_INSERT + readLine);
                Log.d("city file sql : " + readLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void importDatas() {
        String string = App.getContext().getString(R.string.city_data_insert_sql);
        Log.d(Constants.TAG, "city sql : " + string);
        try {
            LitePal.getDatabase().execSQL(string);
        } catch (Exception unused) {
            importDatas2();
        }
    }

    public static City getById(int i) {

        String id = String.valueOf(i);
        City c = LitePal.where("id=?", id).findFirst(City.class);


        return c;

    }

    public static List<City> getByProvinceId(long j) {

        String id = String.valueOf(j);
        List<City> list_c = LitePal.where("provinceId=?", id).find(City.class);

        return list_c;
    }

    public static int getCityIdByName(String str) {
        if (TextUtils.isEmpty(str)) {
            return 0;
        }

        City c = LitePal.where("name=?", str).findFirst(City.class);

        if (c == null){
            return 0;
        }

        return c.getId().intValue();

//        return c.getCityIndex();

    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCityIndex(int cityIndex) {
        this.cityIndex = cityIndex;
    }

    public void setProvinceId(int provinceId) {
        this.provinceId = provinceId;
    }


    public int getCityIndex() {
        return cityIndex;
    }

    public int getProvinceId() {
        return this.provinceId;
    }

    public String getName() {
        return this.name;
    }


}